<template>
  <el-header height="56px">
    <div style="font-weight: 700; margin-left: 10px">智慧学习平台</div>
    <div>
      <span style="line-height:40px; margin-right: 20px;">欢迎{{ user.username }}回来</span>
      <el-dropdown>
        <el-avatar :size="40" :src="circleUrl"/>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>{{ user.role.roleName }}</el-dropdown-item>
            <el-dropdown-item v-if="user.username === 'admin'" @click="handleUpdate">更新学校平面图</el-dropdown-item>
            <el-dropdown-item @click="handleExit">退出</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </el-header>

  <el-dialog v-model="dialogVisible" title="更新学校平面图" width="50%">
    <el-upload class="upload-demo" drag action="/adminapi/upload" :data="data" :on-success="handleSuccess">
      <el-icon class="el-icon--upload">
        <upload-filled/>
      </el-icon>
      <div class="el-upload__text">
        将文件拖到此处或 <em>点击上传</em>
      </div>
      <template #tip>
        <div class="el-upload__tip">
          jpg/png files with a size less than 500kb
        </div>
      </template>
    </el-upload>
  </el-dialog>
</template>

<script setup>
import {useUserStore} from '../../store/useUserStore'
import {useRouterStore} from '../../store/useRouterStore'
import {useRouter} from 'vue-router'
import {ref} from 'vue'
import {UploadFilled} from '@element-plus/icons-vue'

const {changeUser, user} = useUserStore()
const {changeRouter} = useRouterStore()
const router = useRouter()
const circleUrl = new URL('../../assets/avatar.png', import.meta.url).href
const handleExit = () => {
  changeUser({})
  changeRouter(false)
  //跳转到登录
  router.push("/login")
}
//更新平面图
const dialogVisible = ref(false)
const handleUpdate = () => {
  dialogVisible.value = true
}
const data = {
  "token": localStorage.getItem("token")
}
const handleSuccess = () => {
  dialogVisible.value = false;
  window.location.reload();
}
</script>

<style lang="scss" scoped>
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
}
</style>